package com.example.demo.demotwo.optional;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class OptionalDemo {


    @Test
    public void createOptional(){
        Optional<Integer> optional01 = Optional.of(1);
        Optional<Object> o = Optional.ofNullable(null);
        Optional<Object> empty = Optional.empty();
    }

    @Test
    public void isPresentTest(){
        Optional<Integer> optional1 = Optional.ofNullable(1);
        Optional<Object>  optional2 = Optional.ofNullable(null);
        Optional<Object>  optional3 = Optional.ofNullable(Arrays.asList(null,1));
        //Optional<Object>  optional3 = Optional.of(null);
        System.out.println(optional1.isPresent());
        System.out.println(optional2.isPresent());
        System.out.println(optional3.isPresent());
        //System.out.println(optional3);
    }

    @Test
    public void ifPresentTest(){
        Optional<Integer> optional1 = Optional.ofNullable(1);
        Optional<Object>  optional2 = Optional.ofNullable(Arrays.asList(null,1));
        Optional<Object>  optional3 = Optional.ofNullable(null);
        Optional<List<Integer>> optional4 = Optional.of(Arrays.asList(null, 1));
        optional1.ifPresent(t-> System.out.println("optional1:" + t));
        optional2.ifPresent(t-> System.out.println("optional2:" + t));
        optional3.ifPresent(t-> System.out.println("optional3:" + t));
        optional4.ifPresent(t-> System.out.println("optional4:" + t));
    }


    @Test
    public void getTest(){
        //Optional<String> optional1 = Optional.of("javaone");
        Optional<String> optional1 = Optional.of(null);

        if(optional1.isPresent()){
            String s = optional1.get();
            System.out.println(s);
        }
    }

    @Test
    public void orElseTest(){
        String nullName = null;
        String default_name = Optional.ofNullable(nullName).orElse("default_name");
        System.out.println(default_name);
        String hasName = "zwzheng";
        String default_name01 = Optional.ofNullable(nullName).orElse("default_name");
        System.out.println(hasName);
   }

    @Test
    public void orElseGetTest(){
        String text = null;
        String defaultText = Optional.ofNullable(text).orElseGet(this::getDefaultValue);
        
    }

    public String getDefaultValue(){
        System.out.println("Getting Default Value");
        return "Default Value";
    }



}
